package com.example.android.plugin.framework.util;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import com.example.android.plugin.framework.beans.Plugin;
import com.example.android.plugin.framework.beans.PluginFeatureBean;
import com.example.android.plugin.framework.beans.PluginFeatureMethod;
import com.example.android.plugin.framework.beans.PluginIntent;

import android.util.Xml;

/**
 * 解析xml
 */
public class XMLParse {

	public static interface XmlElement {
		public static final String _PLUGIN = "plugin-features";
		public static final String _FEATURE = "feature";
		public static final String _METHOD = "method";
		public static final String _DESCRIPTION = "description";
		public static final String _NEEDCTX = "need-context";
		public static final String _NAME = "name";
		public static final String _NAME_SPACE = "";
		public static final String _INTENT = "intent";
		public static final String _ACTION = "action";
		public static final String _KEY = "key";
		public static final String _SUPER = "super";
		public static final String _FIELD = "field";
		public static final String _TYPE = "type";
	}

	public XMLParse() {
	}

	/**
	 * 解析xml组装纯类名文本描述的插件
	 * 
	 * @param ins
	 * @return
	 * @throws IOException
	 * @throws XmlPullParserException
	 */
	public Plugin parsePluginXML(InputStream ins) throws IOException,
			XmlPullParserException {
		XmlPullParser parser = Xml.newPullParser();
		parser.setInput(ins, "utf-8");

		Plugin plugin = new Plugin();

		List<PluginFeatureBean> features = new ArrayList<PluginFeatureBean>();

		Map<String, PluginIntent> intents = new HashMap<String, PluginIntent>();
		plugin.setIntents(intents);

		int event = parser.getEventType();

		PluginFeatureBean feature = new PluginFeatureBean();

		PluginFeatureMethod featureMethod = new PluginFeatureMethod();

		PluginIntent intent = new PluginIntent();

		// 一直走到文档结束
		while (event != XmlPullParser.END_DOCUMENT) {

			switch (event) {
			// 结点标签开始
			case XmlPullParser.START_TAG:

				// 主节点
				if (XmlElement._PLUGIN.equalsIgnoreCase(parser.getName())) {
				}

				// 功能描述结点
				else if (XmlElement._DESCRIPTION.equalsIgnoreCase(parser
						.getName())) {
					plugin.setDescription(parser.getAttributeValue(
							XmlElement._NAME_SPACE, XmlElement._NAME));
					plugin.setSuperDescription(parser.getAttributeValue(
							XmlElement._NAME_SPACE, XmlElement._SUPER));
				}

				// 功能描述结点
				else if (XmlElement._FEATURE.equalsIgnoreCase(parser.getName())) {
					feature.setFeatureName(parser.getAttributeValue(
							XmlElement._NAME_SPACE, XmlElement._NAME));
				}

				// 方法描述结点
				else if (XmlElement._METHOD.equalsIgnoreCase(parser.getName())) {
					featureMethod.setMethodName(parser.getAttributeValue(
							XmlElement._NAME_SPACE, XmlElement._NAME));
					if ("true".endsWith(parser.getAttributeValue(
							XmlElement._NAME_SPACE, XmlElement._NEEDCTX))) {
						featureMethod.setNeedContext(true);
					} else {
						featureMethod.setNeedContext(false);
					}
					featureMethod.setDescription(parser.nextText());
					event = parser.getEventType();
					if (event == XmlPullParser.END_TAG) {
						continue;
					}
				} else if (XmlElement._INTENT
						.equalsIgnoreCase(parser.getName())) {
					intent.setAction(parser.getAttributeValue(
							XmlElement._NAME_SPACE, XmlElement._ACTION));
					intent.setKey(parser.getAttributeValue(
							XmlElement._NAME_SPACE, XmlElement._KEY));
					intent.setDescription(parser.nextText());
					event = parser.getEventType();
					if (event == XmlPullParser.END_TAG) {
						continue;
					}
				}
				break;

			// 节点表前结束
			case XmlPullParser.END_TAG:
				// 方法描述结点
				if (XmlElement._METHOD.equalsIgnoreCase(parser.getName())) {
					feature.addMethod(featureMethod);
					featureMethod = new PluginFeatureMethod();
				}
				// 功能描述结点
				else if (XmlElement._FEATURE.equalsIgnoreCase(parser.getName())) {
					features.add(feature);
					feature = new PluginFeatureBean();
				}
				// intent
				else if (XmlElement._INTENT.equalsIgnoreCase(parser.getName())) {
					plugin.putIntent(intent.getKey(), intent);
					intent = new PluginIntent();
				}
				// 主节点
				else if (XmlElement._PLUGIN.equalsIgnoreCase(parser.getName())) {
				}
				break;
			}

			event = parser.next();
		}

		ins.close();

		plugin.setFeatures(features);
		return plugin;

	}

}
